package com.ITArmy.controller.admin;

import com.ITArmy.beans.Serialize;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.controller.base.BaseController;
import com.ITArmy.dao.BlogMapper;
import com.ITArmy.dao.CommentMapper;
import com.ITArmy.dao.SerializeMapper;
import com.ITArmy.service.SerializeService;
import com.ITArmy.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Api(value = "后台连载接口层次", tags = "后台连载接口层次Controller层")
@Controller
@RequestMapping("/admin/Serialize")
public class AdminSerializeController extends BaseController {

    @RequestMapping("/addSerialize")
    @ResponseBody
    @ApiOperation(value = "后台添加连载信息，",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse addSerialize(Serialize serialize) {
        serialize.setsTime(new Date());
        return serializeService.addSerialize(serialize);
    }

    @RequestMapping("/list")
    @ApiOperation(value = "遍历连载信息",
            httpMethod = "POST", response = ServerResponse.class)
    public String list(@RequestParam(value = "page", defaultValue = "1") int page, Model model) {
        PageHelper.startPage(page,9);
        List<Serialize> serializes = serializeService.findAllSerialize();
        PageInfo PageInfo = new PageInfo<>(serializes,9);
        model.addAttribute("serializeList", PageInfo);
        return "serialize";
    }

    @RequestMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "修改信息时显示原始信息",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse edit(Long id) {
        return serializeService.findAllSerializeById(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改连载信息，", notes = "管理员可修改下面除了ID的所有的参数，用户只能修改标题，介绍，封面，类型",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse updateBlog(Serialize serialize) {
        return serializeService.updateSerialize(serialize);
    }

    @RequestMapping("/findAllSerializeByTitle")
    @ApiOperation(value = "根据标题模糊查询，",
            httpMethod = "POST", response = ServerResponse.class)
    public String findAllSerializeByTitle(String title, Model model) {
        ServerResponse serializeServerResponse = serializeService.findAllSerializeByTitle(title);
        model.addAttribute("serializeListByTitle", serializeServerResponse);
        return "serialize";
    }

    @RequestMapping("/findAllSerializeByType")
    @ApiOperation(value = "根据连载类型搜索，", notes = "类型用固定选择",
            httpMethod = "POST", response = ServerResponse.class)

    public String findAllSerializeByType(Integer tid, Model model) {
        ServerResponse serializeServerResponse = serializeService.findAllSerializeByType(tid);
        model.addAttribute("serializeListByType", serializeServerResponse);
        return "serialize";
    }

    @RequestMapping("/recommend")
    @ResponseBody
    @ApiOperation(value = "是否设置为推荐，", notes = "推荐为1，不推荐为0",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse recommend(Long id, Long number) {
        return serializeService.recommend(id, number);
    }

    @RequestMapping("/putaway")
    @ResponseBody
    @ApiOperation(value = "是否上架，", notes = "上架为1，不上架为0",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse putaway(Long id, Long number,String email) {
        return serializeService.putaway(id, number,email);
    }

    @RequestMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除连载信息，",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse deleteSerialize(Long id) {
        return serializeService.deleteSerialize(id);
    }

    @RequestMapping("/deleteMany")
    @ResponseBody
    @ApiOperation(value = "批量删除连载信息，",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse deleteMany(String ids) {
        String[] id = ids.split(",");
        return serializeService.deleteManyByIds(id);
    }
}
